//
//  MahjongGameControlView.m
//  Mahjong
//
//  Created by 饶首建 on 17/7/24.
//  Copyright © 2017年 com.ikamobile.com. All rights reserved.
//

#import "MahjongGameControlView.h"
#import "HWPopTool.h"

#import "MahjongAttributeCVC.h"
#import "MahjongAlertConfirmView.h"

@interface MahjongGameControlView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIGestureRecognizerDelegate>
@property (nonatomic,strong) UIWindow *window;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArr;
@property (weak, nonatomic) IBOutlet UILabel *otherTitleLabel;

@end
static NSString *kMahjongAttributeCVC = @"kMahjongAttributeCVC";
@implementation MahjongGameControlView
- (void)awakeFromNib{
    [super awakeFromNib];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        [[HWPopTool sharedInstance]closeWithBlcok:^{}];
    }];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[MahjongAttributeCVC class] forCellWithReuseIdentifier:kMahjongAttributeCVC];
}

+(instancetype)mahjongGameControlView{
    return [[[NSBundle mainBundle] loadNibNamed:@"MahjongGameControlView"
                                          owner:nil options:nil]lastObject];
}

- (void)configData{
    _dataArr = [NSMutableArray new];
    if (_controlType == ControlTypeHu) {//胡
        [_dataArr addObject:@[@"平胡",@"自摸",@"杠上花",@"杠上炮",@"大对子",@"清一色",@"七对",@"龙七对"]];
        [_dataArr addObject:@[@"带根",@"加番"]];
        [_dataArr addObject:_matchModel.optionalArr];
    }else{//查
        [_dataArr addObject:@[@"平胡",@"大对子",@"清一色",@"七对",@"龙七对"]];
        [_dataArr addObject:@[@"带根",@"加番"]];
        [_dataArr addObject:_matchModel.optionalArr];
    }
    
    _titleLabel.text = _controlType == ControlTypeHu?@"胡":@"查叫";
    _selectedAttributeArr = [NSMutableArray new];

    
    
    [_collectionView reloadData];
 
    _otherTitleLabel.hidden = _dataArr.count<3;
    
    NSString *mid = [[NSUserDefaults standardUserDefaults]stringForKey:@"MATCH_ID"];
    if (![mid isEqualToString:_matchModel.matchId]) {//_controlType == ControlTypeHu &&
        [self alertAction];
    }
}


#pragma mark - UICollectionView
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return _dataArr.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSArray *arr = _dataArr[section];
    return arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MahjongAttributeCVC *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kMahjongAttributeCVC forIndexPath:indexPath];
    NSString *attr = _dataArr[indexPath.section][indexPath.row];
    cell.titleLabel.text = attr;
    return cell;
}
#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    NSString *attr = _dataArr[indexPath.section][indexPath.row];
    MahjongAttributeCVC *cell = (MahjongAttributeCVC*)[collectionView cellForItemAtIndexPath:indexPath];
    if ([_selectedAttributeArr containsObject:attr]) {
        [self stateForCell:cell selected:NO];
        [_selectedAttributeArr removeObject:attr];
    }else{
        [self stateForCell:cell selected:YES];
        [_selectedAttributeArr addObject:attr];
    }
}

- (void)stateForCell:(MahjongAttributeCVC*)cell selected:(BOOL)flag{
    cell.layer.borderColor = flag?UIColorHex(F15A5A).CGColor:UIColorHex(9B9B9B).CGColor;
    cell.backgroundColor = flag?UIColorFromRGBA(0xF15A5A,0.1):[UIColor whiteColor];
    cell.titleLabel.textColor = flag?UIColorHex(F15A5A):UIColorHex(9B9B9B);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(150,74);
}
//section的内边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

//section之间的行距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}
//item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeMake(300,20);
}

- (IBAction)sureAction:(id)sender {
    NSString *url = NSStringFormat(@"%@/Games/%@/actions",BASE_URL,_gameModel.gameId);
    NSMutableArray *tmp = [NSMutableArray new];
    for (PlayerModel *pm in _selectedPlayerArr) {
        [tmp addObject:@{
                         @"id":pm.playerId,
                         @"name":pm.playerName
                         }];
    }
    
    NSDictionary *param = @{
                            @"type":_controlType == ControlTypeHu?@"胡":@"查叫",
                            @"attributes":_selectedAttributeArr,
                            @"owner":@{
                                    @"id":_owner.playerId,
                                    @"name":_owner.playerName
                                    },
                            @"targets":tmp
                            };
    [HttpUtil sendRequestURL:url method:POST param:param success:^(id response) {
        [[HWPopTool sharedInstance] closeWithBlcok:^{}];
    } fail:^(NSString *errorStr) {
        [Hud hudText:errorStr inView:self];
    }];
}

- (IBAction)closeAction:(id)sender {
    [[HWPopTool sharedInstance] closeWithBlcok:^{}];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    // 取消响应手势
    if (touch.view == self){
        return YES;
    }
    return NO;
}

- (void)alertAction{
    MahjongAlertConfirmView *mv = [MahjongAlertConfirmView mahjongAlertConfirmView];
    mv.alertTextLabel.text = @"已经为您计算了杠出来的牌,\n请不用再勾选【带根】牌型";
    UIViewController *vc = [[UIViewController alloc]init];
    vc.view.backgroundColor = UIColorFromRGBA(0x000000, 0.5);
    [vc.view addSubview:mv];
    mv.center = vc.view.center;
    _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    _window.windowLevel = UIWindowLevelAlert + 10000;
    _window.opaque = NO;
    _window.rootViewController = vc;
    [_window makeKeyAndVisible];
    mv.neverAlertClickBlock = ^(){
        [_window removeFromSuperview];
        _window = nil;
        [[NSUserDefaults standardUserDefaults]setValue:_matchModel.matchId forKey:@"MATCH_ID"];
    };
    mv.knowClickBlock = ^(){
        [_window removeFromSuperview];
        _window = nil;
    };
}


@end
